//
//  HDInfoShowVC.m
//  HDSmartPenSDKDemo
//
//  Created by 牛的mini on 2016/12/26.
//  Copyright © 2016年 牛的mini. All rights reserved.
//

#import "HDInfoShowVC.h"

@interface HDInfoShowVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSArray *statusArray;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@end

@implementation HDInfoShowVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.title = @"硬件配置信息";
    
    _dataArray = @[@"ID",@"电量",@"LED灯颜色",@"音量",@"语种",@"蓝牙状态",@"红外状态",@"RFID状态",@"MP3播放状态",@"防盗报警状态"];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reloadStatusAction:)
                                                 name:kBeacon_SMPen_All_Info_Notification
                                               object:nil];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self requestSMPenInfoAction:nil];
}

- (void)reloadStatusAction:(NSNotification *)noti
{
    NSArray *statusArray = noti.object;
    if (statusArray.count > 0)
    {
        _statusArray = statusArray;
        [_myTableView reloadData];
    }
    [LCCoolHUD hideInView:self.view];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ return _dataArray.count;}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger nowRow = indexPath.row;
    NSString *identifier = @"HDInfoShowVCCellA";
    if (nowRow >= 5 )
    { identifier = @"HDInfoShowVCCellB"; }
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.textLabel.text = _dataArray[nowRow];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger nowRow = indexPath.row;
    if (nowRow >= 5 )
    {
        UISwitch *nowSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
        [nowSwitch setOn:NO];
        if (_statusArray.count > nowRow)
        {
            NSString *statusStr = _statusArray[nowRow];
            if (statusStr.integerValue == 1)
                [nowSwitch setOn:YES];
        }
        nowSwitch.userInteractionEnabled = NO;
        cell.accessoryView = nowSwitch;
    }
    else if (_statusArray.count > nowRow)
    {
        NSString *statusStr = _statusArray[nowRow];
        cell.detailTextLabel.text = statusStr;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        [[HDBeaconManager sharedBeacon] searchSMPenIDAction];
    }else if (indexPath.row == 1){
        [[HDBeaconManager sharedBeacon] readPowerAction];
    }
}

- (IBAction)requestSMPenInfoAction:(id)sender {
    [[HDBeaconManager sharedBeacon] searchSMPenIDAction];
    [[HDBeaconManager sharedBeacon] performSelector:@selector(readAllInfoAction) withObject:nil afterDelay:0.5];
    [LCCoolHUD showLoading:@"正在获取" inView:self.view];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
